from tkinter import *
from prettytable import PrettyTable as PT
import pickle

pickle_file_a = open('admin.pkl','rb')
pickle_file_u = open('user.pkl','rb')
pickle_file_h = open('user_hobby.pkl','rb')
admin_list = pickle.load(pickle_file_a)
ord_list = pickle.load(pickle_file_u)
ord_dict = pickle.load(pickle_file_h)


def chuchun():
    pickle_file_a = open('admin.pkl', 'wb')
    pickle_file_u = open('user.pkl', 'wb')
    pickle_file_h = open('user_hobby.pkl', 'wb')
    pickle.dump(admin_list, pickle_file_a)
    pickle.dump(ord_list, pickle_file_u)
    pickle.dump(ord_dict, pickle_file_h)

def admin_show_to_check():
    windows.destroy()
    admin_check()



def user_hobby():
    def back():
        package.destroy()
        show_menu()

    def back_save():
        chuchun()
        change_tk.destroy()
        show_menu()

    def change():
        global change_tk
        hobby_new = e3.get()
        ord_dict[name] = hobby_new
        package.destroy()
        change_tk = Tk()
        table = PT(['账号', '密码', '爱好'])
        for temp in ord_list:
            if temp[0] == name:
                table.add_row([temp[0], temp[1], ord_dict[name]])

        Label(change_tk,text='您修改后的账号情况如下：').grid(row=0,column=0)
        Label(change_tk,text=table).grid(row=1,column=1)

        Button(change_tk, text='取消保存并退出', width=20, command=quit).grid(row=2, column=0)
        Button(change_tk, text='保存并返回上一级菜单', width=20, command=back_save).grid(row=2, column=1)

    windows.destroy()
    package = Tk()
    Label(package,text=f'您当前的爱好是：{ord_dict[name]} ').grid(row=0,column=0)
    Label(package,text='请输入您的新爱好').grid(row=1,column=0)

    e3 = Entry(package)
    e3.grid(row=1,column=1)

    Button(package, text='确定', width=20,command=change).grid(row=2,column=0)
    Button(package, text='取消并返回上一级菜单', width=20,command=back).grid(row=2,column=1)

def admin_check():
    def del_user():
        def del_backto_check():
            admin_del.destroy()
            admin_check()


        def del_save():
            def del_to_back():
                admin_del.destroy()
                admin_del_s1.destroy()
                admin_check()

            def del_backto_check2():
                admin_del_s.destroy()
                admin_check()

            def del_to_save():
                for i in range(len(ord_list)):
                    if ord_list[i][0] == names:
                        del ord_list[i]
                        ord_dict.pop(names)
                        chuchun()
                        admin_del_s.destroy()
                        admin_check()

            names = e11.get()
            flagx = 0
            for temp in ord_list:
                if temp[0] == names:
                    flagx = 1
                    admin_del.destroy()
                    admin_del_s = Tk()
                    Label(admin_del_s, text=f'确定删除用户 {names} 吗').grid(row=0, column=0)

                    Button(admin_del_s, text='确定', width=20, command=del_to_save).grid(row=2, column=0)
                    Button(admin_del_s, text='取消', width=20, command=del_backto_check2).grid(row=2, column=1)

            if flagx == 0:
                admin_del_s1 = Tk()
                Label(admin_del_s1, text='您想要删除的用户不存在或已经删除').grid()
                Button(admin_del_s1, text='返回', width=20, command=del_to_back).grid(row=2, column=0)


        admin_begin.destroy()
        admin_del = Tk()
        Label(admin_del, text='删除用户').grid(row=0, column=0)
        Label(admin_del, text='请输入你想要删除的账户id：').grid(row=1, column=0)

        e11 = Entry(admin_del)
        e11.grid(row=1, column=1)

        Button(admin_del, text='确定', width=20, command=del_save).grid(row=2, column=0)
        Button(admin_del, text='取消', width=20, command=del_backto_check).grid(row=2, column=1)


    def admin_alter_user():
        def alter_backto_check():
            admin_alter.destroy()
            admin_check()

        def alter_save():
            named1 = e7.get()
            paswd1 = e8.get()
            hobby1 = e9.get()

            flag1 = 0
            for temp in ord_list:
                if temp[0] == named1:
                    flag1 = 1
                    temp[1] = paswd1
                    ord_dict[named1] = hobby1


            if flag1 == 0:
                ord_list.append([named1,paswd1])
                ord_dict[named1] = hobby1

            chuchun()
            admin_alter.destroy()
            admin_check()

        global named1,paswd1,hobby1
        admin_begin.destroy()
        admin_alter = Tk()
        admin_alter.geometry('600x500')
        Label(admin_alter, text='修改账户(若不存在则新建用户)').place(x=50, y=50)
        Label(admin_alter, text='请输入你想要修改账户id：').place(x=100, y=100)
        Label(admin_alter, text='请输入该用户的新密码：').place(x=100, y=200)
        Label(admin_alter, text='请输入该用户的新爱好：').place(x=100, y=300)

        e7 = Entry(admin_alter, width=20)
        e8 = Entry(admin_alter, width=20)
        e9 = Entry(admin_alter, width=20)
        e7.place(x=300, y=100)
        e8.place(x=300, y=200)
        e9.place(x=300, y=300)


        Button(admin_alter, text='确定', width=20, command=alter_save).place(x=100, y=400)
        Button(admin_alter, text='取消', width=20, command=alter_backto_check).place(x=300, y=400)



    def admin_add():
        # def destroy_worning():
        #     admin_worning.destroy()

        def aback_to_show():
            admin_app.destroy()
            show_menu()

        def check_save():
            def back_to_add():
                check_a.destroy()
                admin_check()

            names = e4.get()
            for temp in ord_list:
                if names == temp[0]:
                    admin_app.destroy()
                    check_a = Tk()
                    Label(check_a, text='该用户已存在，请检查后重新输入').grid(row=0, column=0)
                    Button(check_a, text='确定', width=20, command=back_to_add).grid(row=1, column=0)

            paswd = e5.get()
            hobby = e6.get()
            ord_list.append([names,paswd])
            ord_dict[names] = hobby
            chuchun()
            admin_app.destroy()
            admin_check()

        # destroy_worning()
        admin_begin.destroy()
        admin_app = Tk()
        Label(admin_app, text='请在对应位置输入新的用户信息').grid(row=0, column=0)
        Label(admin_app, text='请输入新的用户账号').grid(row=1, column=0)
        Label(admin_app, text='请输入新的用户密码').grid(row=2, column=0)
        Label(admin_app, text='请输入新的用户爱好').grid(row=3, column=0)

        e4 = Entry(admin_app)
        e5 = Entry(admin_app)
        e6 = Entry(admin_app)

        e4.grid(row=1, column=1)
        e5.grid(row=2, column=1)
        e6.grid(row=3, column=1)

        Button(admin_app, text='确定', width=20, command=check_save).grid(row=4, column=0)
        Button(admin_app, text='取消', width=20, command=aback_to_show).grid(row=4, column=1)

    def aback_to_show0():
        admin_begin.destroy()
        show_menu()

    # global admin_worning
    # if len(ord_list) == 0:
    #     admin_worning = Tk()
    #     admin_worning.title('worning!')
    #     Label(admin_worning,text='当前不存在普通用户，请立即新建').grid()
    #
    #     Button(admin_worning, text='新建用户', width=20,command=admin_add).grid(row=2, column=0)
    #
    # else:
    table = PT(['账号', '密码', '爱好'])
    for temp in ord_list:
        table.add_row([temp[0], temp[1], ord_dict[temp[0]]])

    admin_begin = Tk()
    Label(admin_begin, text='当前文件下所有用户如下：').grid(row=0,column=0)
    Label(admin_begin, text=table).grid(row=1,column=1)

    Button(admin_begin, text='新建账号', width=20, command=admin_add).grid(row=2, column=1)
    Button(admin_begin, text='修改账号', width=20, command=admin_alter_user).grid(row=3, column=1)
    Button(admin_begin, text='删除账号', width=20, command=del_user).grid(row=4, column=1)
    Button(admin_begin, text='返回上一级菜单', width=20, command=aback_to_show0).grid(row=5, column=1)
    Button(admin_begin, text='退出程序', width=20, command=quit).grid(row=6, column=1)





def show_menu():
    global windows
    windows = Tk()
    windows.geometry('500x150')
    Label(windows, text='请选择你下一步想要执行的操作').grid(column=0)
    if flag == 1:
        Button(windows, text='查看所有人员名单', width=20,command=admin_show_to_check).grid(column=1)
        Button(windows, text='返回上一级菜单', width=20,command=land).grid(column=1)
        Button(windows, text='退出', width=20, command=quit).grid(column=1)

    else:
        Button(windows, text='修改当前爱好', width=20,command=user_hobby).grid(column=1)
        Button(windows, text='返回上一级菜单', width=20,command=land).grid(column=1)
        Button(windows, text='退出', width=20, command=quit).grid(column=1)


def land0():
    root.destroy()
    land()

def land():
    def land1():
        def landc():
            root0.destroy()
            root3.destroy()
            land()

        def ord_user():
            root2.destroy()
            root0.destroy()
            show_menu()

        def admin_user():
            root1.destroy()
            root0.destroy()
            show_menu()

        global flag,name
        name = e1.get()
        pswd = e2.get()

        if (name, pswd) in admin_list:
            flag = 1
            root1 = Tk()
            Label(root1, text="管理员登陆成功！").pack()
            Button(root1, text='完成', width=20, command=admin_user).pack()

        elif [name, pswd] in ord_list:
            flag = 0
            root2 = Tk()
            Label(root2, text=f"用户 {name} 登陆成功").grid()
            Button(root2, text='完成', width=20, command=ord_user).grid()


        else:
            root3 = Tk()
            Label(root3, text="登陆失败！请检查用户名和密码的输入").pack()
            Button(root3, text='重新登陆', width=20, command=landc).pack()


    root0 = Tk()
    root0.title('用户管理系统')
    root0.geometry('500x150')

    Label(root0, text='用户名 :').grid(row=2, column=0)
    Label(root0, text='密 码 :').grid(row=3, column=0)

    e1 = Entry(root0)
    e2 = Entry(root0)

    e1.grid(row=2, column=1)
    e2.grid(row=3, column=1)

    Button(root0, text='登陆', width=20, command=land1).grid(row=5, column=0, sticky=W, padx=10, pady=10)
    Button(root0, text='退出', width=20, command=root.quit).grid(row=5, column=1, sticky=W, padx=10, pady=10)

root = Tk()
root.title('用户管理系统')
Label(root, text='欢迎来到用户管理系统，请输入您的用户名和密码').pack()
Button(root, text='开始程序', width=20, command=land0).pack()
mainloop()